﻿/*
Copyright (C) 2009  Tomasz Chrzanowski

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Com.TomCdc.ChangeDataCapture
{
    internal class TreeNodeColumn:TreeNode
    {
        /// <summary>
        /// The icon representing disabled database column.
        /// </summary>
        private const string ICON_DBCOLDISABLED = "DbColDisabled.ico";

        /// <summary>
        /// The icon representing available for tracking database column.
        /// </summary>
        private const string ICON_DBCOLSTD = "DbColStd.ico";

        /// <summary>
        /// The icon representing tracking included database column.
        /// </summary>
        private const string ICON_DBCOLINC = "DbColInc.ico";

        /// <summary>
        /// The icon representing tracking excluded database column.
        /// </summary>
        private const string ICON_DBCOLEXC = "DbColExc.ico";

        /// <summary>
        /// The icon representing primary key column.
        /// </summary>
        private const string ICON_DBCOLPRIMKEY = "DbColPrimKey.ico";

        private SourceColumn _sqlSrcCol;
        /// <summary>
        /// Gets the object representing source column.
        /// </summary>
        internal SourceColumn SqlSourceColumn
        {
            get
            {
                return _sqlSrcCol;
            }
        }

        /// <summary>
        /// Gets a value indicating wheather the column is currently tracked.
        /// True if currently tracked, otherwise false.
        /// </summary>
        internal bool IsTracked
        {
            get
            {
                return _sqlSrcCol.IsTracked;
            }
        }

        /// Gets or sets a value indicating wheather the column is currently tracked.
        /// When set, column gets checked/unchecked and node icon is reset to reflect the change.
        /// True if currently tracked, otherwise false.
        internal bool ActivateTracking
        {
            get
            {
                return _sqlSrcCol.ActivateTracking;
            }

            set
            {
                _sqlSrcCol.ActivateTracking = value;
                Checked = value;
                SetNodeIcon();
            }
        }

        /// <summary>
        /// Gets a value indicating whether the column is disabled.
        /// </summary>
        internal bool IsDisabled
        {
            get
            {
                return _sqlSrcCol.IsDisabled;
            }
        }

        /// <summary>
        /// Gets a value indicating whether the column is a primary key. The property set to true,
        /// when no primary key in the table, but this column contains unique values (uniqe constraint).
        /// </summary>
        internal bool IsPrimaryKey
        {
            get
            {
                return _sqlSrcCol.IsPrimaryKey;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TreeNodeColumn"/> class.
        /// </summary>
        /// <param name="srcColumn">A source column the class will represent.</param>
        internal TreeNodeColumn(SourceColumn srcColumn)
        {
            // validates input argument
            if (srcColumn == null)
            {
                throw new ArgumentNullException("srcColumn");
            }

            _sqlSrcCol = srcColumn;

            // prepares column description
            string colTypeLen = "";
            // checks if column is of data type where precission has to be displayed
            if(_sqlSrcCol.MaxLength > 0)
            {
                // create string with data type precison in round bracket
                colTypeLen = string.Format("({0})", _sqlSrcCol.MaxLength);
            }

            // combines column name, data type and precission if needed
            this.Text = string.Format("{0} ({1}{2})", _sqlSrcCol.ColumnName, _sqlSrcCol.DataType, colTypeLen);
            
            // if already tracked, set check box
            this.Checked = _sqlSrcCol.IsTracked;

            SetNodeIcon();
        }

        /// <summary>
        /// Sets a tree node icon by analyzing state of the column.
        /// </summary>
        private void SetNodeIcon()
        {
            // by default set standard icon
            SetIcon(ICON_DBCOLSTD);

            // if activated but not currently tracked, set as "included"
            if (_sqlSrcCol.ActivateTracking && !_sqlSrcCol.IsTracked)
            {
                SetIcon(ICON_DBCOLINC);
            }

            // if not activated but is currently tracked, set as "excluded"
            if (!_sqlSrcCol.ActivateTracking && _sqlSrcCol.IsTracked)
            {
                SetIcon(ICON_DBCOLEXC);
            }

            // if column disabled (not allowed data type for tracking), set as "disabled"
            if (_sqlSrcCol.IsDisabled)
            {
                SetIcon(ICON_DBCOLDISABLED);
            }

            // if primary key (or unique constraint on column if no primary key exists), set as "primary key"
            if (_sqlSrcCol.IsPrimaryKey)
            {
                SetIcon(ICON_DBCOLPRIMKEY);
            }
        }

        /// <summary>
        /// Sets the node icon, provided in the argument. Sets node properties SelectedImageKey and ImageKey, 
        /// thanks to it the same icon is displayed for selected and not selected node.
        /// </summary>
        /// <param name="iconName">The name of an icon existing in <see cref="uxImageList16"/> object.</param>
        private void SetIcon(string iconName)
        {
            // validates input column name
            if (string.IsNullOrEmpty(iconName))
            {
                throw new ArgumentException("The iconName argument must not be empty or null.");
            }

            // the same icon needed when node selected and  not selected
            this.SelectedImageKey = iconName;
            this.ImageKey = iconName;
        }
    }
}
